﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;
using System.Collections;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for UC_ORG_Section.xaml
    /// </summary>
    public partial class UC_ORG_Section : UserControl
    {
        static string id = "0";
        public UC_ORG_Section()
        {
            InitializeComponent();
            //Loading Data in Grid
            BindDataToGrid();
            //Loading section InCharge from Employee master
            LoadSectionInCharge();
            //Loading Location from Location Master
            LoadLocation();
            //To Get identity Id
            GetIdentity();
            //To Name The Section Master New or Edit Mode
            lblNewrEdit.Content = "New";
        }
        public void LoadSectionInCharge()
        {
            DataTable ENamedt = new DataTable();
            ENamedt = SectionMasterDAL.GetORGSectionInchargeName();
            cmbSecInCharge.DataContext = ENamedt.DefaultView;
            cmbSecInCharge.DisplayMemberPath = ENamedt.Columns["EName"].ToString();
            cmbSecInCharge.SelectedValuePath = ENamedt.Columns["EId"].ToString();
            ArrayList Namelist = ArrayList.Adapter(cmbSecInCharge.Items);
            Namelist.Sort();
        }
        public void GetIdentity()
        {
            DataTable dt = new DataTable();
            dt = SectionMasterDAL.GetORGSectionIdentity();
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString() == "0")
                {
                    lblSecIdDisplay.Text = "1";
                }
                else
                {
                    lblSecIdDisplay.Text = (Int32.Parse(dt.Rows[0][0].ToString()) + 1).ToString();
                }
            }
        }

        public void LoadLocation()
        {
            DataTable Locdt = new DataTable();
            Locdt = SectionMasterDAL.GetORGSectionUpdateLocName();
            cmbLoc.DataContext = Locdt.DefaultView;
            cmbLoc.DisplayMemberPath = Locdt.Columns["LocationName"].ToString();
            cmbLoc.SelectedValuePath = Locdt.Columns["LocationID"].ToString();
        }
        public void BindDataToGrid()
        {
            DataTable GetLocNameNSectionName = new DataTable();
            GetLocNameNSectionName = SectionMasterDAL.GetORGLoadLocNameSecInChrge();
            DataRow dtrow;
            for (int i = 0; i < 10; i++)
            {
                dtrow = GetLocNameNSectionName.NewRow();
                dtrow["SectionId"] = DBNull.Value;
                dtrow["SectionName"] = "";
                dtrow["Description"] = "";
                dtrow["Location"] = "";
                dtrow["Section Incharge"] = "";
                dtrow["Status"] = null;
                GetLocNameNSectionName.Rows.Add(dtrow);
            }
                GrdSectionData.ItemsSource = GetLocNameNSectionName;
        }

        public void ClearFields()
        {
            txtSecName.Text = "";
            txtDesc.Text = "";
            cmbLoc.SelectedIndex = -1;
            cmbSecInCharge.SelectedIndex = -1;
            chkIsActive.IsChecked = true;
            GrdSectionData.SelectedItem = null;
            id = "0";
            lblNewrEdit.Content = "New";
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateForm())
            {

                MessageBoxResult msgRes = MessageBox.Show("Do you want to Save this Section Information", "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                if (msgRes == MessageBoxResult.Yes)
                {
                    int ischk;
                    if (chkIsActive.IsChecked == true)
                    {
                        ischk = 1;
                    }
                    else
                    {
                        ischk = 0;
                    }


                    if (id == "0")
                    {
                        SectionMasterDAL.GetSectionMasterInsert(txtSecName.Text, txtDesc.Text, Int32.Parse(cmbLoc.SelectedValue.ToString()), cmbSecInCharge.SelectedValue.ToString(), ischk);
                        MessageBox.Show("Information Inserted Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                        BindDataToGrid();

                        ClearFields();
                        GetIdentity();
                    }
                    else
                    {

                        SectionMasterDAL.GetSectionMasterUpdate(Int32.Parse(lblSecIdDisplay.Text), txtSecName.Text, txtDesc.Text, Int32.Parse(cmbLoc.SelectedValue.ToString()), cmbSecInCharge.SelectedValue.ToString(), ischk);
                        MessageBox.Show("Information Updated Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                        BindDataToGrid();

                        ClearFields();
                        GetIdentity();
                        id = "0";

                    }                    
                }
            }
        }
        #region Validation Before insertion
        public bool ValidateForm()
        {
            lblSecName.Foreground = System.Windows.Media.Brushes.Black;
            lblLoc.Foreground = System.Windows.Media.Brushes.Black;
            lblSecInCharge.Foreground = System.Windows.Media.Brushes.Black;
            if (txtSecName.Text == "")
            {
                MessageBox.Show("Enter Section Name", "Section Name", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtSecName.Focus();
                lblSecName.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblSecName.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbLoc.SelectedIndex == -1)
            {
                MessageBox.Show("Select Location", "Location", MessageBoxButton.OK, MessageBoxImage.Warning);
                lblLoc.Foreground = System.Windows.Media.Brushes.Red;
                cmbLoc.Focus();
                return false;
            }
            else
            {
                lblLoc.Foreground = System.Windows.Media.Brushes.Black;
            }
            if (cmbSecInCharge.SelectedIndex == -1)
            {
                MessageBox.Show("Select Section In Charge", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbSecInCharge.Focus();
                lblSecInCharge.Foreground = System.Windows.Media.Brushes.Red;
                return false;
            }
            else
            {
                lblSecInCharge.Foreground = System.Windows.Media.Brushes.Black;
            }
            return true;

        }
        #endregion

        private void radGrdData_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            lblNewrEdit.Content = "Edit";
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value==null)
            {                
                btnClear_Click(this, e);
                return;                
            }
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            lblSecIdDisplay.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            txtSecName.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtDesc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            cmbLoc.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString();
            cmbSecInCharge.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();
            if ((((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString()) == "No")
            {
                chkIsActive.IsChecked = false;
            }
            else
            {
                chkIsActive.IsChecked = true;
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            ClearFields();
            GetIdentity();
        }

        private void txtSecName_LostFocus(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
            dt = SectionMasterDAL.GetORGSectionSecNameCompare(Int32.Parse(lblSecIdDisplay.Text), txtSecName.Text);
            if (!(dt.Rows[0][0].ToString() == "0"))
            {

                MessageBox.Show("Section Name " + txtSecName.Text + " Already Utilized,Please give another Section Name", "Duplicate Section Name", MessageBoxButton.OK, MessageBoxImage.Error);
                txtSecName.Text = "";
                txtSecName.Focus();
                lblSecName.Foreground = System.Windows.Media.Brushes.Red;
            }
            else
            {
                lblSecName.Foreground = System.Windows.Media.Brushes.Black;
            }
        }        
    }
}
